package com.tenement.controller.backend;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.tenement.base.constants.SysmConstants.state;
import com.tenement.base.exception.ParameterException;
import com.tenement.mybatis.entity.User;
import com.tenement.service.interfaces.IUserService;

@Controller
public class UserManageController {

	private static final Log logger = LogFactory.getLog(AdminController.class);

	@Autowired
	private IUserService iUserService;

	/**
	 * 进入用户管理页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "toUserManageHtml.action")
	public String toUserManageHtml(HttpServletRequest request) {
		List<User> userList = iUserService.getUserList();
		request.setAttribute("userList", userList);
		return "view/backend_system/user_list";
	}

	/**
	 * 重置用户密码
	 * 
	 * @param a_id
	 * @return
	 */
	@RequestMapping(value = "resetUserPwd.action")
	public @ResponseBody String resetUserPwd(String uId, HttpServletRequest request) {
		logger.info(uId);
		return state.Failed;
	}

	/**
	 * 删除用户
	 * 
	 * @param a_id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "deleteUser.action")
	public @ResponseBody String deleteAdmin(String uId, HttpServletRequest request) {
		try {
			if (uId == null || "".equals(uId)) {
				throw new ParameterException("【参数uId为空】");
			}
			boolean b = iUserService.deleteUserByKey(Integer.parseInt(uId));
			if (b) {
				return state.Success;
			} else {
				return state.Failed;
			}
		} catch (ParameterException p) {
			p.printStackTrace();
		}
		return state.Failed;
	}

}
